package com.base.system.base.db.specifications;

import javax.persistence.criteria.*;

public class BetweenSpecification<T> extends AbstractSpecification<T> {

    private static final long serialVersionUID = 2151135671022659384L;
    private final String property;
    private final Object lowValue;
    private final Object upValue;

    public BetweenSpecification(String property, Object lowValue, Object upValue, JoinType joinType) {
        this.property = property;
        this.lowValue = lowValue;
        this.upValue = upValue;
        this.joinType = joinType;
    }

    @Override
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
        From from = getFrom(property, root);
        String attr = getProperty(property);
        return cb.between(from.get(attr), (Comparable) lowValue, (Comparable) upValue);
    }
}
